package com.baosight.sczx.zm.cg.service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.ch.domain.ZMCHHoleReport;
/**
 * 抽采进度日汇报
 */
public class ServiceZMCG85 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p>
	 * <p>return value: ServiceZMCG85</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:45 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));
		outInfo.addBlock(StaticBlocks.getStauts4());
		return outInfo;
	}
	
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:26:59 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCHHoleReport.query", new ZMCHHoleReport());
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNum", false));
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", false,UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(StaticBlocks.getStauts4());
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		return outInfo;
	}	
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:31:06 张小飞
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCHHoleReport _dom = new ZMCHHoleReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i)); 
			//校验是否重复
			Map<String, String>	params = new HashMap<String, String>();
			boolean validate = this.validate(inInfo, _dom,"ZMCHHoleReport.validate",params);
			if(validate) {
				inInfo = this.query(inInfo);
				return inInfo;
			}
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CH_HOLEREPORT"));
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
			_dom.setFdReportDepartmentId(UserSession.getInSessionProperty("orgCode").toString());// 汇报部门id
//			_dom.setFdReportDepartment(UserSession.getInSessionProperty("orgName").toString());// 汇报部门
			_dom.setFdReporterId(UserSession.getUserId().toString());// 汇报人ID
			_dom.setFdReporter(UserSession.getInSessionProperty("UserCname").toString());// 汇报人
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
			
			this.dao.insert("ZMCHHoleReport.insert",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 
	}
	
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:59:23 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {		
		EiInfo out=new EiInfo(); 
		ZMCHHoleReport _dom = new ZMCHHoleReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			// 校验是否重复
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			boolean validate = this.validate(inInfo, _dom, "ZMCHHoleReport.validate", params);
			if (validate) {
				out = this.query(inInfo);
				return out;
			}
			this.dao.update("ZMCHHoleReport.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功"); 
		return out; 
	}	
	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCH20</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午04:12:10 张小飞
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = null;
		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++)
		{
			EiInfo _temp = super.delete(inInfo, "ZMCHHoleReport.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		return outInfo;
	}	
	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-22上午10:59:40 张小飞
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{
		EiInfo out=new EiInfo(); 
		ZMCHHoleReport _dom = new ZMCHHoleReport();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
		    this.dao.update("ZMCHHoleReport.report",_dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	
	}
	
	/**
	 * <p>Function: validate</p>
	 * <p>Description: 对主表记录新增，删除，上报前的后台校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-10-21下午03:40:23 张小飞
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, ZMCHHoleReport _dom, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		String fdWorknumId = _dom.getFdWorknumId();
		String fdMiningId = _dom.getFdMiningId();
		String fdCurrentDate = _dom.getFdCurrentDate();
		params.put("fdWorknumId", fdWorknumId);
		params.put("fdMiningId", fdMiningId);
		params.put("fdCurrentDate", fdCurrentDate);
		List result = dao.query("ZMCHHoleReport.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "抽采进度日汇报每矿每班每天只能填写一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}
